<?php
/**
 * JSON 转 数组方法!
 * 先json_decode再用~
 * PHP5.3 之后 可以传入json_decode($obj,true) 即可不用此函数!
 * @param  [array|object] $array [description]
 * @return [array]        [description]
 */
function object_array($array){
   if(is_object($array)){
      $array = (array)$array;
   };
   if(is_array($array)){
    foreach($array as $key=>$value){
      $array[$key] = object_array($value);
    };
   };
   return $array;
}

/**
 * 获取目录的结构
 * @author 李俊 <duguying2008@gmail.com>
 * @param  [string] $path [目录路径]
 * @return [array]       [目录结构数组]
 */
function dirtree($path) {
	set_time_limit(0);
	$handle = opendir($path);
	$itemArray=array();
	while (false !== ($file = readdir($handle))) {
		if (($file=='.')||($file=='..')){
		}elseif (is_dir($path.$file)) {
				try {
					$dirtmparr=dirtree($path.$file.'/');
				} catch (Exception $e) {
					$dirtmparr=null;
				};
	        	$itemArray[iconv('GB2312','UTF-8',$file)]=$dirtmparr;
	        }else{
	        	array_push($itemArray, iconv('GB2312','UTF-8',$file));
	        }
	    }
	return $itemArray;
}
/**
 * 删除非空的文件夹
 * @param unknown $dir
 */
function rrmdir($dir) {
	if (is_dir($dir)) {
		$objects = scandir($dir);
		foreach ($objects as $object) {
			if ($object != "." && $object != "..") {
				if (filetype($dir."/".$object) == "dir") 
					rrmdir($dir."/".$object); 
				else 
					unlink($dir."/".$object);
			}
		}
		reset($objects);
		return rmdir($dir);
	}else {
		return false;
	}
}
/**
 * 压缩文件
 * @param string $path 需要压缩的文件[夹]路径
 * @param string $savedir 压缩文件所保存的目录
 * @return array zip文件路径
 */
function zip($path,$savedir) {
    $path=preg_replace('/\/$/', '', $path);
    preg_match('/\/([\d\D][^\/]*)$/', $path, $matches, PREG_OFFSET_CAPTURE);
    $filename=$matches[1][0].".zip";
    set_time_limit(0);
    $zip = new ZipArchive();
    $zip->open($savedir.'/'.$filename,ZIPARCHIVE::OVERWRITE);
    if (is_file($path)) {
        $path=preg_replace('/\/\//', '/', $path);
        $base_dir=preg_replace('/\/[\d\D][^\/]*$/', '/', $path);
        $base_dir=addcslashes($base_dir, '/:');
        $localname=preg_replace('/'.$base_dir.'/', '', $path);
        $zip->addFile($path,$localname);
        $zip->close();
        return $filename;
    }elseif (is_dir($path)) {
        $path=preg_replace('/\/[\d\D][^\/]*$/', '', $path);
        $base_dir=$path.'/';//基目录
        $base_dir=addcslashes($base_dir, '/:');
    }
    $path=preg_replace('/\/\//', '/', $path);
    function addItem($path,&$zip,&$base_dir){
        $handle = opendir($path);
        while (false !== ($file = readdir($handle))) {
            if (($file!='.')&&($file!='..')){
                $ipath=$path.'/'.$file;
                if (is_file($ipath)){//条目是文件
                    $localname=preg_replace('/'.$base_dir.'/', '', $ipath);
                    var_dump($localname);
                    $zip->addFile($ipath,$localname);
                } else if (is_dir($ipath)){
                    addItem($ipath,$zip,$base_dir);
                    $localname=preg_replace('/'.$base_dir.'/', '', $ipath);
                    var_dump($localname);
                    $zip->addEmptyDir($localname);
                }
            }
        }
    }
    addItem($path,$zip,$base_dir);
    $zip->close();
    return $filename;
}
?> 
